
package com.ukao.steward.pesenter.logistics;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.ReceiveOrderBean;
import com.ukao.steward.bean.ScanCodeTabBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.T;
import com.ukao.steward.view.logistics.ScanCodeView;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class ScanCodePesenter extends BasePresenter<ScanCodeView> {
    private boolean isloaderr;

    public ScanCodePesenter(ScanCodeView view) {
        attachView(view);
    }



    /**
     * 查询员工操作订单列表
     * @param isEnterFactory  是否是进厂
     * @param pageNum
     * @param pageSize
     */
    public void scanCodeTabele(boolean  isEnterFactory,int pageNum,int pageSize) {

        Observable observable = null;
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("oprType",isEnterFactory?"FACTORY_IN":"FACTORY_OUT");
        mHashMap.put("pageNum",pageNum+"");
        mHashMap.put("pageSize",pageSize+"");
        mHashMap.put("accessToken", SaveParams.getToken());
        observable = apiStores.logWorkerOprOrder(Constant.createParameter(mHashMap));// - 扫码列表
        addSubscription(observable,
                new ApiCallback<ScanCodeTabBean>() {
                    @Override
                    public void onSuccess(ScanCodeTabBean model) {

                        if (model.getHttpCode() == 200) {
                        mvpView.scanCodeSucceed(model.getData());

                        } else {
                            T.show(model.getMsg());
                        }
                    }


                    @Override
                    public void onFinish() {
                        mvpView.loadSucceed();
                    }
                });
    }


    /**
     *  删除操作列表
     */
    public void deleteByOprType(boolean  isEnterFactory) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("oprType",isEnterFactory?"FACTORY_IN":"FACTORY_OUT");
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.deleteByOprType(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {

                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                        mvpView.deleteByOprSucceed();

                        } else {
                        T.show(model.getMsg());

                        }


                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }
                });
    }



    /**
     *  接收订单
     * @param orderNo  订单号运单号
     * @param isEnterFactory
     */
    public void receive(String orderNo,boolean  isEnterFactory) {

        mvpView.showLoading();
        Observable observable = null;
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderNo",orderNo );
        mHashMap.put("accessToken", SaveParams.getToken());
        if (isEnterFactory){
            observable = apiStores.factoryInWithAlert(Constant.createParameter(mHashMap));
        }else{
            observable = apiStores.factoryOutWithAlert(Constant.createParameter(mHashMap));
        }
        addSubscription(observable,
                new ApiCallback<ReceiveOrderBean>() {


                    @Override
                    public void onSuccess(ReceiveOrderBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode() == 200) {
                            ReceiveOrderBean.DataBean mData = model.getData();
                            if (mData!=null&&mData.getAlert()){
                                mvpView.receiveFail(false, mData.getMsg());
                            }else {
                                mvpView.receiveSuccceed();
                            }

                        } else if (model.getHttpCode() == 400) {
                            mvpView.receiveFail(true,model.getMsg());
                        }




                    }

                    @Override
                    public void onFinish() {
                    mvpView.receiveFinish();

                    }
                });
    }



}
